﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class PaiRoomDAL
    {
        public PaiRoomDAL(HoteContent hoteContent)
        {
            HoteContent = hoteContent;
        }

        public HoteContent HoteContent { get; }
        /// <summary>
        /// 排房添加
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public int PaiRoomAdd(PaiRoomMoodel info)
        {
            try
            {
                HoteContent.paiRoomMoodels.Add(info);
                return HoteContent.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        
        /// <summary>
        /// 显示查询分页
        /// </summary>
        /// <param name="totalCount">页容量</param>
        /// <param name="totalPage">页码</param>
        /// <param name="Pageindex">当前页</param>
        /// <param name="PageSize">页码</param>
        /// <returns></returns>
        public IQueryable GetPaiRooList(out int totalCount, out int totalPage, int Pageindex = 1, int PageSize = 2)
        {
            try
            {
                var list = HoteContent.paiRoomMoodels.Join(HoteContent.roomNumberModels, a => a.RoomNumberId, b => b.Id, (a, b) => new
                {
                    a.Id,
                    a.RoomNumberId,
                    a.IsDelete,
                    a.Createtime,
                    a.YiPai,
                    a.WeiPai,
                    a.State,
                    a.PaiRoomTypeId,
                    b.NumberName,
                    
                }).Join(HoteContent.roomTypeModels, a => a.PaiRoomTypeId, b => b.Id, (a, b) => new
                {
                    a.Id,
                    a.RoomNumberId,
                    a.IsDelete,
                    a.Createtime,
                    a.YiPai,
                    a.WeiPai,
                    a.State,
                    a.NumberName,
                    
                    a.PaiRoomTypeId,
                    b.RoomTypeName
                }).Where(o => o.IsDelete == false);
                totalCount = list.Count();
                totalPage = (int)Math.Ceiling(totalCount * 1.0 / PageSize);
                list = list.OrderByDescending(o => o.Id).Skip((Pageindex - 1) * PageSize).Take(PageSize);
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int PaiRoomDel(int id)
        {
            try
            {
                var res = HoteContent.paiRoomMoodels.Find(id);
                if (res != null)
                {
                    res.IsDelete = true;
                }
                return HoteContent.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 批量逻辑删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int PaiRoomDelAll(string ids)
        {
            try
            {
                var res = ids.Split(',');
                foreach (var id in res)
                {
                    var info = HoteContent.paiRoomMoodels.Find(Convert.ToInt32(id));
                    info.IsDelete = true;
                }
                return HoteContent.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 批量修改状态
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public int PaiRoomUptAll(string ids, int state)
        {
            try
            {
                var res = ids.Split(',');
                foreach (var id in res)
                {
                    var info = HoteContent.paiRoomMoodels.Find(Convert.ToInt32(id));
                    info.State = state;
                }
                return HoteContent.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// Id查询(反填)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public PaiRoomMoodel GetPaiRoomId(int id)
        {
            try
            {
                return HoteContent.paiRoomMoodels.Find(id);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public int GetRPaiRoomUpdate(PaiRoomMoodel info)
        {
            try
            {
               
                HoteContent.paiRoomMoodels.Update(info);
                return HoteContent.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
